//
//  HeadView.m
//  ebook
//
//  Created by 吴超飞 on 16/3/8.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "HeadView.h"
#import "UIView+Extension.h"
#import "UIImage+blurring.h"
#import "GuestLoginManager.h"

@interface HeadView(){
    
    UIImageView *bgImageview;
}

@end

@implementation HeadView

- (id)initWithCoder:(NSCoder*)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        
        self.isLogin = [[NSUserDefaults standardUserDefaults] boolForKey:kIsLogin];
        //异步模糊
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            UIImage *fuzzyImg = [[UIImage imageNamed:@"headview_bg"] blurring];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self.img_banner.image = fuzzyImg;
                [self.img_banner setNeedsDisplay];
            });
        });
        [self updateView];
    }
    return self;
}

-(void)drawRect:(CGRect)rect{
   
    BOOL isLogin = [[NSUserDefaults standardUserDefaults] boolForKey:kIsLogin];
    BOOL isguest = [[NSUserDefaults standardUserDefaults] boolForKey:kIsGuest];
    
    self.loginBtn.layer.cornerRadius = 15.0f;
    self.loginBtn.layer.backgroundColor = isLogin?[UIColor clearColor].CGColor:[UIColor colorWithWhite:0.01 alpha:0.2].CGColor;
    if(isLogin&&!isguest)self.loginBtn.titleLabel.font = [UIFont systemFontOfSize:22];
    
    CGRect loginFrame = self.loginBtn.frame;
    loginFrame.size.width = isLogin?250:100;
    loginFrame.origin.x = (MTScreenW - loginFrame.size.width)/2;
    self.loginBtn.frame = loginFrame;
    [self.loginBtn setNeedsLayout];
    
    self.guestInfoBtn.layer.cornerRadius = 15.0f;
    self.guestInfoBtn.layer.backgroundColor = !isguest?[UIColor clearColor].CGColor:[UIColor colorWithWhite:0.01 alpha:0.1].CGColor;
}

- (IBAction)loginBtnClick:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(headView:loginBtnClick:)]) {
        [self.delegate headView:self loginBtnClick:_isLogin];
    }
}

- (IBAction)blindRegister:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(headView:blindGuestBtnClick:)]) {
        [self.delegate headView:self blindGuestBtnClick:sender];
    }
}

//根据当前是否登陆和是否游客登录设置视图状态
-(void)updateView{
    
    BOOL isLogin = [[NSUserDefaults standardUserDefaults] boolForKey:kIsLogin];
    BOOL isguest = [[NSUserDefaults standardUserDefaults] boolForKey:kIsGuest];
    self.loginBtn.enabled = !isLogin;
    NSString *user = [[NSUserDefaults standardUserDefaults] objectForKey:kUserName];
    if(isguest){
        NSString *guest = [GuestLoginManager guestName];
        user=[NSString stringWithFormat:@"游客%@", guest.length>4?[guest substringFromIndex:guest.length-4]:@"a123"];
    }
    [self.loginBtn setTitle:isLogin?[NSString stringWithFormat:@"%@",user]:@"登录/注册" forState:UIControlStateNormal];
    _guestInfoBtn.hidden = !isguest;
    
    [self setNeedsDisplay];
}

-(void)setOffsetY:(float)y
{
    UIView* bannerSuper = _img_banner.superview;
    CGRect bframe = bannerSuper.frame;
    if(y<0)
    {
        bframe.origin.y = y;
        bframe.size.height = -y + bannerSuper.superview.frame.size.height;
        bannerSuper.frame = bframe;
        
        CGPoint center =  _img_banner.center;
        center.y = bannerSuper.frame.size.height/2;
        _img_banner.center = center;
        
        CGPoint scenter = _showView.center;
        scenter.y = bannerSuper.centerY;
        _showView.center = scenter;
    }else{
        
        if(bframe.origin.y != 0)
        {
            bframe.origin.y = 0;
            bframe.size.height = bannerSuper.superview.frame.size.height;
            bannerSuper.frame = bframe;
        }
        if(y<bframe.size.height)
        {
            CGPoint scenter = _showView.center;
            scenter.y = bannerSuper.centerY;
            _showView.center = scenter;
        }
    }
}


@end
